package com.hunan.api.catv.service.biz.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.MemberAssets;
import com.hunan.api.catv.entity.vo.MemberAssetsVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.account.mapstruct.AccountMapstruct;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.biz.system.SystemUserBiz;
import com.hunan.api.catv.service.impl.account.AccountService;
import com.hunan.api.catv.service.impl.member.MemberService;
import com.hunan.api.catv.utils.EasyExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: wangchao
 * @Date: 2020/10/22 11:27
 */
@Service
public class AccountBiz extends ResultService {

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    AccountService accountService;

    @Autowired
    AccountMapstruct accountMapstruct;

    @Autowired
    SystemUserBiz systemUserBiz;

    @Autowired
    MemberService memberService;

    /**
     * 获取cms列表数据
     */
    public PageInfo<MemberAssetsVo> getMemberAccountList(int pageNum, int pageSize, String nickName, Integer status, Integer deleted, String startCreateTime, String endCreateTime, String phone) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(getMemberAssetsList(nickName, status, deleted, startCreateTime, endCreateTime, phone));
    }

    private List<MemberAssetsVo> getMemberAssetsList(String nickName, Integer status, Integer deleted, String startCreateTime, String endCreateTime, String phone) {
        MemberAssetsVo memberAssetsVo = new MemberAssetsVo();
        memberAssetsVo.setNickName(nickName);
        memberAssetsVo.setStatus(status);
        memberAssetsVo.setDeleted(deleted);
        memberAssetsVo.setStartCreateTime(startCreateTime);
        memberAssetsVo.setEndCreateTime(endCreateTime);
        memberAssetsVo.setPhone(phone);
        List<MemberAssetsVo> memberAssetsList = accountService.getMemberAssetsList(memberAssetsVo);
        memberAssetsList = memberAssetsList.stream().map(memberAssetsVo1 -> {
            Map<String, Object> stringObjectMap = memberService.inviterCount(memberAssetsVo1.getMemberId());
            memberAssetsVo1.setInviteTotalNums(Integer.parseInt(String.valueOf(stringObjectMap.get("count"))));
            return memberAssetsVo1;
        }).collect(Collectors.toList());
        return memberAssetsList;
    }

    /**
     * 保存用户资产信息
     *
     * @param memberAssets
     * @return
     */
    public ServiceResult saveMemberAssentsInfo(MemberAssets memberAssets) {
        memberAssets.setCreateTime(new Date());
        memberAssets.setUpdateTime(new Date());
        boolean insert = memberAssets.insert();
        if (!insert) {
            return buildFail(false, ConstantsMsg.admin_add_fail);
        }
        return buildOK(true, ConstantsMsg.admin_add_success);
    }

    /**
     * 修改用户资产信息
     *
     * @param memberAssets
     * @return
     */
    public ServiceResult updateMemberAssentsInfo(MemberAssets memberAssets) {
        MemberAssets memberAssets1 = new MemberAssets().selectOne(new QueryWrapper<MemberAssets>().eq(MemberAssets.MEMBER_ID, memberAssets.getMemberId()));
        if (memberAssets1 == null) {
            return buildFail(false, "");
        }
        memberAssets.setId(memberAssets1.getId());
        memberAssets.setUpdateTime(new Date());
        boolean update = memberAssets.updateById();
        if (!update) {
            return buildFail(false, ConstantsMsg.admin_edit_fail);
        }
        return buildOK(true, ConstantsMsg.admin_edit_success);
    }

    /**
     * 查询用户资产信息
     *
     * @param memberId
     * @return
     */
    public ServiceResult getMemberAssentsInfo(Integer memberId) {
        MemberAssets memberAssets = new MemberAssets().selectOne(new QueryWrapper<MemberAssets>().eq(MemberAssets.MEMBER_ID, memberId));
        if (memberAssets == null) {
            return buildFail("", ConstantsMsg.MEMBER_ASSESTS_NOT_EXIST);
        }
        return buildOK(memberAssets);
    }


    /**
     * 修改用户资产状态
     *
     * @param id
     * @param status
     * @return
     */
    public ServiceResult updateStatus(Integer id, Integer status, Integer sysUserId) {
        MemberAssets memberAssets = new MemberAssets().selectById(id);
        if (memberAssets == null) {
            return buildFail("", ConstantsMsg.MEMBER_ASSESTS_NOT_EXIST);
        }
        if (!memberAssets.setStatus(status).setUpdateBy(sysUserId).setUpdateTime(new Date()).updateById()) {
            return buildFail(false, ConstantsMsg.admin_edit_fail);
        }
        return buildOK(true, ConstantsMsg.admin_edit_success);
    }

    /**
     * 获取用户资产信息
     */
    public MemberAssetsVo getMemberAccount(Integer id) {
        MemberAssets memberAssets = new MemberAssets().selectById(id);
        MemberAssetsVo memberAssetsVo = accountMapstruct.memberAssetsConvertMemberAssetsVo(memberAssets);
        memberAssetsVo.setCreateName(accountService.getSystemUserName(memberAssets.getCreateBy()));
        memberAssetsVo.setUpdateName(accountService.getSystemUserName(memberAssets.getUpdateBy()));
        return memberAssetsVo;
    }

    /*private String getSystemUserName(Integer userId){
        String systemUserName = null;
        if(userId != null){
            systemUserName = systemUserBiz.info(userId).getSysUserName();
        }
        return systemUserName;
    }*/

    /**
     * 逻辑删除
     */
    public ServiceResult delete(Integer id, Integer deleted, Integer sysUserId) {
        MemberAssets memberAssets = new MemberAssets().selectById(id);
        if (memberAssets == null) {
            return buildFail("", ConstantsMsg.MEMBER_ASSESTS_NOT_EXIST);
        }
        if (!memberAssets.setDeleted(deleted).setUpdateBy(sysUserId).setUpdateTime(new Date()).updateById()) {
            return buildFail(false, ConstantsMsg.admin_del_fail);
        }
        return buildOK(true, ConstantsMsg.admin_del_success);
    }

    public ServiceResult exportMemberAssets(String nickName, Integer status, Integer deleted, String startCreateTime, String endCreateTime, String phone, HttpServletResponse response) {
        //导出列表
        EasyExportUtils.exportExcel(getMemberAssetsList(nickName, status, deleted, startCreateTime, endCreateTime, phone), "", "sheet1", MemberAssetsVo.class, "用户资产.xlsx", response);
        return buildOK(true);
    }
}
